package com.screen.screendisplay.service.impl.al;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.screen.screendisplay.entity.al.AlProdoperPrdQual;
import com.screen.screendisplay.mapper.al.AlProdoperPrdQualMapper;
import com.screen.screendisplay.service.al.AlProdoperPrdQualService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.screen.screendisplay.vo.al.AlProdoperPrdQualOneVO;
import com.screen.screendisplay.vo.al.AlProdoperPrdQualThreeVO;
import com.screen.screendisplay.vo.al.AlProdoperPrdQualTwoVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 生产质量表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class AlProdoperPrdQualServiceImpl extends ServiceImpl<AlProdoperPrdQualMapper, AlProdoperPrdQual> implements AlProdoperPrdQualService {

    @Override
    public List<AlProdoperPrdQualOneVO> getAluminumQualityData() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        LocalDate startDate = currentDate.minusMonths(11);
        
        // 查询最近12个月的数据
        QueryWrapper<AlProdoperPrdQual> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("year", startDate.getYear())
                .orderByAsc("year", "month", "day");
        
        List<AlProdoperPrdQual> dataList = this.list(queryWrapper);
        
        // 转换为VO
        List<AlProdoperPrdQualOneVO> result = new ArrayList<>();
        for (AlProdoperPrdQual data : dataList) {
            // 过滤出最近12个月的数据
            LocalDate dataDate = LocalDate.of(data.getYear(), data.getMonth(), 1);
            if (!dataDate.isBefore(startDate)) {
                AlProdoperPrdQualOneVO vo = new AlProdoperPrdQualOneVO();
                vo.setYear(data.getYear());
                vo.setMonth(data.getMonth());
                vo.setDay(data.getDay());
                vo.setAlCurrentQualityRate(data.getAlCurrentQualityRate());
                vo.setAlYoyQualityRate(data.getAlYoyQualityRate());
                result.add(vo);
            }
        }
        
        return result;
    }

    @Override
    public List<AlProdoperPrdQualTwoVO> getLowIronAluminumRateData() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        LocalDate startDate = currentDate.minusMonths(11);
        
        // 查询最近12个月的数据
        QueryWrapper<AlProdoperPrdQual> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("year", startDate.getYear())
                .orderByAsc("year", "month", "day");
        
        List<AlProdoperPrdQual> dataList = this.list(queryWrapper);
        
        // 转换为VO
        List<AlProdoperPrdQualTwoVO> result = new ArrayList<>();
        for (AlProdoperPrdQual data : dataList) {
            // 过滤出最近12个月的数据
            LocalDate dataDate = LocalDate.of(data.getYear(), data.getMonth(), 1);
            if (!dataDate.isBefore(startDate)) {
                AlProdoperPrdQualTwoVO vo = new AlProdoperPrdQualTwoVO();
                vo.setYear(data.getYear());
                vo.setMonth(data.getMonth());
                vo.setDay(data.getDay());
                vo.setLowFeAlCurrentRate(data.getLowFeAlCurrentRate());
                vo.setLowFeAlYoyRate(data.getLowFeAlYoyRate());
                result.add(vo);
            }
        }
        
        return result;
    }

    @Override
    public List<AlProdoperPrdQualThreeVO> getCarbonAnodeQualityData() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        LocalDate startDate = currentDate.minusMonths(11);
        
        // 查询最近12个月的数据
        QueryWrapper<AlProdoperPrdQual> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("year", startDate.getYear())
                .orderByAsc("year", "month", "day");
        
        List<AlProdoperPrdQual> dataList = this.list(queryWrapper);
        
        // 转换为VO
        List<AlProdoperPrdQualThreeVO> result = new ArrayList<>();
        for (AlProdoperPrdQual data : dataList) {
            // 过滤出最近12个月的数据
            LocalDate dataDate = LocalDate.of(data.getYear(), data.getMonth(), 1);
            if (!dataDate.isBefore(startDate)) {
                AlProdoperPrdQualThreeVO vo = new AlProdoperPrdQualThreeVO();
                vo.setYear(data.getYear());
                vo.setMonth(data.getMonth());
                vo.setDay(data.getDay());
                vo.setCarbonAnodeCurrentRate(data.getCarbonAnodeCurrentRate());
                vo.setCarbonAnodeYoyRate(data.getCarbonAnodeYoyRate());
                result.add(vo);
            }
        }
        
        return result;
    }
}
